package com.reader.cms.controller.sys;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.entity.sys.SysRegions;
import com.reader.cms.service.sys.ISysRegionsService;
import com.reader.cms.controller.BaseController;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description   功能描述
 * @author        jiahaikun
 * @date          2018-08-06
 */
@RestController
@RequestMapping("/api/sysregions")
public class SysRegionsController extends BaseController {
private final Logger logger = LoggerFactory.getLogger(SysRegionsController.class);

@Autowired
public ISysRegionsService iSysRegionsService;


/**
 * @function 分页查询数据
 *
 * @param currentPage   当前页
 * @param size   每页记录数
 * @param param   其它查询条件，根据情况动态添加
 * @return
 */
@GetMapping("getPageList")
public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage ,
                                                @RequestParam(value = "size") int size,
                                                @RequestParam(value = "param") String param) {
       ResultMessageInfo resultMessage=null;
       Page<Map<String, Object>> list=iSysRegionsService.selectListPage(currentPage,size,"");
       if(list.getTotal()>0){
          resultMessage = ResultMessageInfo.ofSuccess("成功",list);
       }
       else {
           return ResultMessageInfo.ofMessage("查不到数据!");
       }

       return resultMessage;
}

/**
 * @function 添加新记录
 * @param sysRegions 实体对象
 * @return
 */
@PostMapping("insert")
public ResultMessageInfo insert(@RequestBody SysRegions sysRegions) {
    ValidatorUtil.validateEntity(sysRegions, AddGroup.class);//非空验证
    ResultMessageInfo resultMessage=null;
    boolean resultValue =  iSysRegionsService.insert(sysRegions);
    if(resultValue){
        resultMessage = ResultMessageInfo.ofSuccess("添加成功!",resultValue);
    }
    else{
        return ResultMessageInfo.ofMessage("添加失败!");
    }
    return resultMessage;
 }

/**
 * @function 修改
 * @param sysRegions 实体对象
 * @return
 */
@PostMapping("update")
@Transactional
@ApiOperation(value="跟据ID列新记录", notes="id为必须项")
@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int",paramType = "path")})
public ResultMessageInfo update(@RequestBody  SysRegions sysRegions) {
    ValidatorUtil.validateEntity(sysRegions, AddGroup.class);//非空验证
    ResultMessageInfo resultMessage=null;
    boolean resultValue =  iSysRegionsService.updateById(sysRegions);
    if(resultValue){
         resultMessage = ResultMessageInfo.ofSuccess("修改成功!",resultValue);
    }
    else{
        return ResultMessageInfo.ofMessage("修改失败!");
    }
    return resultMessage;
}

/**
 * @function 批量删除对象
 * @param data 键为ids值为整型数组的json格式
 * @return
 */
 @PostMapping("deleteIds")
 @Transactional
 @ApiOperation(value="跟据ID删除记录", notes="键为ids值为整型数组的json格式")
 @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "id列表", required = true, dataType = "int",paramType = "path")})
 public ResultMessageInfo deleteBatchIds(@RequestBody String data){
    ResultMessageInfo resultMessage=null;
    List<Long> ids = JSON.parseArray( JSON.parseObject(data).getString("ids"),Long.class);
    boolean resultValue =  iSysRegionsService.deleteBatchIds(ids);
    if(resultValue){
         resultMessage = ResultMessageInfo.ofSuccess("删除成功!",resultValue);
    }
    else{
        return ResultMessageInfo.ofMessage("删除失败!");
    }
    return resultMessage;

 }
}